package com.example.myapplication.request

import androidx.lifecycle.MutableLiveData
import com.example.myapplication.bean.ListResultBean
import com.example.myapplication.bean.article.ArticleBean
import com.example.myapplication.bean.square.UserSharedBean
import com.kun.base.net.Api
import com.kun.base.net.ResponseResult

/**
 *广场相关的请求
 *
 * @author : Kun
 * @date : 2021/6/10
 * @time : 9:48
 */
class SquareRequest {
    /**
     * 广场列表数据返回的结果
     */
    val squareArticleResult = MutableLiveData<ResponseResult<ListResultBean<ArticleBean>>>()

    /**
     * 用户分享数据返回的bean
     */
    val userSharedResult = MutableLiveData<ResponseResult<UserSharedBean>>()

    /**
     * 个人分享数据返回的bean
     */
    val mySharedResult = MutableLiveData<ResponseResult<UserSharedBean>>()

    /**
     * 删除分享文章的结果
     */
    val deleteSharedResult = MutableLiveData<ResponseResult<Int>>()

    /**
     * 分享文章的结果
     */
    val sharedArticleResult = MutableLiveData<ResponseResult<ArticleBean>>()

    /**
     * 请求广场列表数据
     */
    suspend fun requestSquareArticleList(index: Int) {
        squareArticleResult.value = Api.apiCall {
            Api.getInstance(IApi::class.java).getSquareArticleList(index)
        }
    }

    /**
     * 请求用户分享数据
     */
    suspend fun requestUserShared(userId: Int, index: Int) {
        userSharedResult.value = Api.apiCall {
            Api.getInstance(IApi::class.java).getUserShared(userId, index)
        }
    }

    /**
     * 请求个人分享数据
     */
    suspend fun requestMyShared(index: Int) {
        mySharedResult.value = Api.apiCall {
            Api.getInstance(IApi::class.java).getMyShared(index)
        }
    }

    /**
     * 请求删除文章
     */
    suspend fun requestDeleteShared(id: Int) {
        deleteSharedResult.value = Api.apiCall {
            Api.getInstance(IApi::class.java).deleteShared(id)
        }.apply {
            data = id
        }
    }

    /**
     * 请求分享文章
     */
    suspend fun requestSharedArticle(title: String, link: String) {
        sharedArticleResult.value = Api.apiCall {
            Api.getInstance(IApi::class.java).sharedArticle(title, link)
        }
    }
}